package samfeng.erp.product.category.dal.mysql;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import samfeng.erp.product.category.controller.admin.vo.ProductCategoryExportReqVO;
import samfeng.erp.product.category.controller.admin.vo.ProductCategoryPageReqVO;
import samfeng.erp.product.category.dal.dataobject.ProductCategoryDO;
import samfeng.erp.product.category.enmus.CommonConstants;

/**
 * 产品类别 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ProductCategoryMapper extends BaseMapperX<ProductCategoryDO> {

    default PageResult<ProductCategoryDO> selectPage(ProductCategoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProductCategoryDO>()
                .betweenIfPresent(ProductCategoryDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProductCategoryDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ProductCategoryDO::getPicUrl, reqVO.getPicUrl())
                .orderByDesc(ProductCategoryDO::getId));
    }

    default List<ProductCategoryDO> selectList(ProductCategoryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ProductCategoryDO>()
                .betweenIfPresent(ProductCategoryDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProductCategoryDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ProductCategoryDO::getPicUrl, reqVO.getPicUrl())
                .orderByDesc(ProductCategoryDO::getId));
    }

    @Update("update product_product_category set status = #{status} where id = #{id}")
    void updateStateById(@Param("status") String status, @Param("id") String id);

    default List<Long> productCategoryIdList(ProductCategoryDO productCategoryDO) {
        List<Long> gcIdList = new ArrayList<>();
        Long parentId = productCategoryDO.getParentId();
        gcIdList.add(productCategoryDO.getId());
        while (!CommonConstants.PARENT_ID.equals(parentId)) {
            gcIdList.add(0, parentId);
            ProductCategoryDO pGoodsCategory = selectOne(Wrappers.<ProductCategoryDO>lambdaQuery().eq(ProductCategoryDO::getId, parentId));
            parentId = pGoodsCategory.getParentId();
        }
        return gcIdList;
    }

}
